/**
 * @param {number} dividend
 * @param {number} divisor
 * @return {number}
 */

const mathSign = (n: number): -1 | 0 | 1 => {
    if (n === 0) return 0

    return Math.abs(n) === n ? 1 : -1
}


const MAX = 2147483647
var divide = function (dividend: number, divisor: number) {


    const sign = mathSign(dividend) * mathSign(divisor)

    dividend = Math.abs(dividend)
    divisor = Math.abs(divisor)


    if (divisor === 1) {

        const c = dividend
        if (sign > 0) {
            if (c > MAX) return MAX
        }

        if (sign < 0) {
            if (c >= MAX) return MAX+1
        }

        return dividend === 0 ? 0 : dividend * sign
    }

    let c = 0
    while (dividend >= divisor) {
        dividend -= divisor
        c++
    }

    if (sign > 0) {
        if (c > MAX) return MAX
    }

    if (sign < 0) {
        if (c >= MAX) return MAX
    }


    return c === 0 ? 0 : c * sign

};

console.log(divide(-2147483648, -1));
console.log(divide(-2147483648,
    1));